/*
 * VariablesTable.java
 *
 * Created on August 30, 2007, 1:34 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package portfolio.stock;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneLayout;
import javax.swing.table.DefaultTableModel;
import misellanea.MiscellaneaDB;
import portfolio.dbHost.project.Project;
import portfolio.dbHost.project.attributes.TimeLine;
import portfolio.mainScreen;

/**
 *
 * @author ACA
 */
public class VariablesTable extends JInternalFrame
{
    private mainScreen fatherFrame = null;
    private JTable table = null;
    
    /** Creates a new instance of VariablesTable 
     * @param fatherFrameParam 
     * @param resultSetParam 
     * @param sStockNameParam 
     */
    public VariablesTable(mainScreen fatherFrameParam, /*JPanel jTableTab,*/ ResultSet resultSetParam, String sStockNameParam) 
    {
        super (sStockNameParam, true, true, true);
        
        fatherFrame = fatherFrameParam;
        String sStockName = sStockNameParam;
        
        JPanel jTableTab = new JPanel (new BorderLayout ()); //fatherFrame.getTablePanel();
        //JTabbedPane jTableTab = new JTabbedPane ();
        try {
            DefaultTableModel dataTableModel = MiscellaneaDB.transferResultSet2TableModel (resultSetParam, new DefaultTableModel ());        
        
            JPanel tableTabButtons = new JPanel ();
            tableTabButtons.setLayout (new FlowLayout (FlowLayout.RIGHT));

            table = new JTable (dataTableModel);
            table.setAutoResizeMode (JTable.AUTO_RESIZE_OFF);

            JScrollPane jTableScroll = new JScrollPane ();
            jTableScroll.setLayout (new ScrollPaneLayout ());

            jTableScroll.setViewportView (table);
            jTableTab.add (jTableScroll, BorderLayout.CENTER);
            
//////////////////////////////////////////
            Project project = fatherFrame.getDBLoggedHost().getProject();
            String [] sTimeLine = project.getTimeline().get();
            Integer ITerm = new Integer (sTimeLine [TimeLine.TIMELINE_TERM]);
////////////////////////////////////////////////////////////
            int iTerm = ITerm.intValue(); //fatherFrame.getDBLoggedHost().getProject().getTimeline().getTerm();
            int iRowCount = table.getRowCount();
//            System.out.println ("<<<<<<<<<<<<<<<<<<<<<<<<<<<<< iRowCount: " + iRowCount);
            table.setRowSelectionInterval(0, iRowCount - iTerm -1);

//            JButton jAnalizeButton = new JButton ("Analize");
//            SelectButton analizeButton = new SelectButton (fatherFrame, jAnalizeButton, sStockName, table);
////            tableTabButtons.add (analizeButton);
//            tableTabButtons.add (jAnalizeButton);
//            SaveButton saveButton = new SaveButton ("Save query", fatherFrame.getDBLoggedHost().getConnection(), sStockName, table);
//            tableTabButtons.add (saveButton);
//            JButton exitButton = new JButton ("Exit");
//            tableTabButtons.add (exitButton);

            jTableTab.add (tableTabButtons, BorderLayout.NORTH);

            fatherFrame.getLayeredPane().add(this);
            this.setBounds(40, 40, 300, 300);
            this.show ();
        } catch (SQLException ex) {
            fatherFrame.displayMessage("VariablesTable.VariablesTable 87", ex.toString());
            return;
        } catch (Exception ex) {
            fatherFrame.displayMessage("VariablesTable.VariablesTable 90", ex.toString());
            return;
        }        
    }
}

